package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.sumpu.app.guoguojie.dao.CommentDao;
import cn.sumpu.app.guoguojie.domain.Comment;
import cn.sumpu.app.guoguojie.domain.Ext.CommentExt;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class CommentDaoImpl extends SqlSessionDaoSupport implements CommentDao {
	private static Logger logger = LoggerFactory.getLogger(CommentDaoImpl.class);
	public void insert(Comment comment) throws AppBusinessException {
		try {
			getSqlSession().insert(
					"cn.sumpu.app.guoguojie.persistence.CommentMapper.insertComment",
					comment);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<CommentExt> getCommentList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.CommentMapper.getCommentList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	public void updateComment(Comment comment) throws AppBusinessException {
		try {
			 getSqlSession().update(
					"cn.sumpu.app.guoguojie.persistence.CommentMapper.updateComment",
					comment);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update contact failure.", e);
			}
			throw new AppBusinessException("update contact failure.", e);
		}
	}

	public void deleteCommentById(String id) throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.CommentMapper.deleteCommentById",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteLearning failure.", e);
			}
			throw new AppBusinessException("deleteLearning failure.", e);
		}
	}

	@Override
	public Comment getCommentById(String id) throws AppBusinessException {
		try {
			return (Comment) getSqlSession()
					.selectOne("cn.sumpu.app.guoguojie.persistence.CommentMapper.getCommentById",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteLearning failure.", e);
			}
			throw new AppBusinessException("deleteLearning failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<CommentExt> getFeedCommentExtList(Map <String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.CommentMapper.getFeedCommentExtList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	@Override
	public int getCommentCountList(String commentId)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.CommentMapper.getCommentCountList",
							commentId);
			return (Integer) (obj==null?0:obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getBlogCountList failure.", e);
			}
			throw new AppBusinessException("getCommentCountList failure.", e);
		}
	}

}
